package com.gy.zpchat.ui.chat.base.adapter;

import android.app.Activity;
import android.os.Handler;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import im.zego.zim.entity.ZIMMessage;
import im.zego.zim.entity.ZIMRevokeMessage;
import im.zego.zim.entity.ZIMSystemMessage;
import im.zego.zim.enums.ZIMMessageDirection;
import im.zego.zim.enums.ZIMMessageType;
import com.gy.zpchat.ui.chat.base.adapter.holder.ChatCenterViewHolder;
import com.gy.zpchat.ui.chat.base.adapter.holder.ChatLeftViewHolder;
import com.gy.zpchat.ui.chat.base.adapter.holder.ChatRightViewHolder;
import com.gy.zpchat.enums.ChatItemType;
import com.gy.zpchat.entity.MessageInfo;

public class ChatContentAdapter extends RecyclerView.Adapter {
    private OnItemClickListener onItemClickListener;
    public Handler handler;
    private List<MessageInfo> messageInfoList = new ArrayList<>();
    private Activity activity;

    private boolean isSelectMode  = false;
    public ChatContentAdapter (Activity activity){
        this.activity = activity;
    }

    public void addItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
    private HashMap<String, String> userAvatarHashMap = new HashMap<>();

    public void setAvatarMap(HashMap<String,String> avatarMap){
        userAvatarHashMap = avatarMap;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder viewHolder = null;
        if (ChatItemType.CHAT_ITEM_TYPE_RIGHT.value == viewType) {
            viewHolder = new ChatRightViewHolder(parent, onItemClickListener, activity);
        } else if (ChatItemType.CHAT_ITEM_TYPE_LEFT.value == viewType) {
            viewHolder = new ChatLeftViewHolder(parent, onItemClickListener, activity);
        }else if (ChatItemType.CHAT_ITEM_TYPE_CENTER.value == viewType){
            viewHolder = new ChatCenterViewHolder(parent);
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {

        holder.itemView.setTag(position);

        MessageInfo messageInfo = messageInfoList.get(position);

        if (holder instanceof ChatLeftViewHolder) {
            ChatLeftViewHolder chat = (ChatLeftViewHolder) holder;
            chat.setMessageInfo(messageInfo,isSelectMode,userAvatarHashMap.get(messageInfo.getZIMMessage().getSenderUserID()));
        } else if (holder instanceof ChatRightViewHolder) {
            ChatRightViewHolder chat = (ChatRightViewHolder) holder;
            chat.setMessageInfo(messageInfo,isSelectMode,userAvatarHashMap.get(messageInfo.getZIMMessage().getSenderUserID()));
        }else if (holder instanceof ChatCenterViewHolder){
            ChatCenterViewHolder chat = (ChatCenterViewHolder) holder;
            if (messageInfo.getZIMMessage().getType() == ZIMMessageType.REVOKE){
                ZIMRevokeMessage message = (ZIMRevokeMessage) messageInfo.getZIMMessage();
                if (message.getDirection() == ZIMMessageDirection.SEND){
                    chat.setText("你撤回了一条消息");
                }else {
                    chat.setText(message.getSenderUserID()+"撤回了一条消息");
                }
            }else if (messageInfo.getZIMMessage().getType() == ZIMMessageType.SYSTEM){
                ZIMSystemMessage message = (ZIMSystemMessage) messageInfo.getZIMMessage();
                chat.setText(message.message);
            }
        }
    }

    @Override
    public int getItemViewType(int position) {
        return messageInfoList.get(position).getType().value;
    }

    @Override
    public int getItemCount() {
        return messageInfoList.size();
    }

    @Override
    public long getItemId(int position) {
        return messageInfoList.get(position).hashCode();
    }

    public void add(MessageInfo messageInfo) {
        messageInfoList.add(0,messageInfo);
    }

    public void addMessageToBottom(MessageInfo messageInfo){
        messageInfoList.add(messageInfo);
    }

    public void setSelectMode(boolean isSelect){
        isSelectMode = isSelect;
        notifyItemRangeChanged(0,messageInfoList.size());
    }

    public void delete(MessageInfo info) {
        MessageInfo tempInfo = info;
        for (MessageInfo c : messageInfoList) {
            if (c.getZIMMessage().getMessageID() == info.getZIMMessage().getMessageID()) {
                tempInfo = c;
            }
        }
        messageInfoList.remove(tempInfo);
    }

    public void delete(long messageId) {
        MessageInfo tempInfo = null;
        int position = 0;
        for (int i = 0; i < messageInfoList.size(); i++) {
            if (messageInfoList.get(i).getZIMMessage().getMessageID() == messageId) {
                tempInfo = messageInfoList.get(i);
                position = i;
                break;
            }
        }

        if (tempInfo!=null){
            messageInfoList.remove(tempInfo);
            notifyItemRemoved(position);
        }
    }

    public void clearInfo() {
        messageInfoList.clear();
    }

    public MessageInfo getMessage(int index) {
        if (messageInfoList != null && messageInfoList.size() > 0) {
            return messageInfoList.get(index);
        }
        return null;
    }

    public void set(MessageInfo info){
        int position = -1;
        for (int i = 0; i < messageInfoList.size(); i++) {
            if (messageInfoList.get(i).getZIMMessage().getLocalMessageID() == info.getZIMMessage().getLocalMessageID()) {
                position = i;
                break;
            }
        }
        if (position!=-1){
            messageInfoList.set(position,info);
            notifyItemChanged(position);
        }else {
            messageInfoList.add(info);
            notifyItemInserted(messageInfoList.size()-1);
        }
    }

    public void updateMessageWhenDiffSentStatus(MessageInfo info){
        int position = -1;
        for (int i = 0; i < messageInfoList.size(); i++) {
            ZIMMessage oldMessage = messageInfoList.get(i).getZIMMessage();
            ZIMMessage newMessage = info.getZIMMessage();
            if (oldMessage.getLocalMessageID() == info.getZIMMessage().getLocalMessageID() && newMessage.getSentStatus() != oldMessage.getSentStatus()) {
                position = i;
                break;
            }
        }
        if (position!=-1){
            messageInfoList.set(position,info);
            notifyItemChanged(position);
        }
    }

    public List<MessageInfo> getMessageInfoList(){
        return messageInfoList;
    }
}
